package com.franz.noteapp.useCase

import com.franz.noteapp.bean.NoteBean
import com.franz.noteapp.bean.NoteOrder
import com.franz.noteapp.bean.NoteType
import com.franz.noteapp.room.repository.NoteRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
 * 对所有笔记进行各类排序处理*/
class GetNotes(private val repository:NoteRepository) {
    operator fun invoke(noteType: NoteType = NoteType.Date(NoteOrder.Descending)): Flow<List<NoteBean>> {
        return  repository.getNotes().map { notes ->
            when(noteType.noteOrder){
                is NoteOrder.Ascending->{
                    when(noteType){
                        is NoteType.Title-> notes.sortedBy { it.title.lowercase() }
                        is NoteType.Date-> notes.sortedBy { it.time }
                        is NoteType.Color-> notes.sortedBy { it.color }
                    }
                }
                is NoteOrder.Descending->{
                    when(noteType){
                        is NoteType.Title-> notes.sortedByDescending { it.title.lowercase() }
                        is NoteType.Date-> notes.sortedByDescending { it.time }
                        is NoteType.Color-> notes.sortedByDescending { it.color }
                    }
                }
            }
        }
    }
}